<template>
  <div class="login">
    <div class="login-box" ref="div">
      <div class="img_box">
        <img src="../assets/logo.png" alt />
      </div>
      <el-form ref="loginFormRef" :model="loginForm" :rules="rules">
        <el-form-item prop="username">
          <el-input v-model="loginForm.username" prefix-icon="iconfont icon-wode-active"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input type="password" v-model="loginForm.password" prefix-icon="iconfont icon-mima" show-password></el-input>
        </el-form-item>
        <el-form-item class="form_button">
          <el-button type="primary" @click="loginTo">登录</el-button>
          <el-button @click="resetLoginForm">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { login } from '../api/login'
export default {
  data () {
    return {
      // 保存登录的信息
      loginForm: {
        username: 'admin',
        password: '123456'
      },
      // rules定义验证规则的对象
      rules: {
        // 验证规则（注：验证规则的名字必须与input绑定的值一致）
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 6, max: 16, message: '长度在 6 到 16 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    // 使用form表单重置功能，在el-form-item上必须有prop属性
    resetLoginForm () {
      this.$refs.loginFormRef.resetFields()
    },
    loginTo () {
      // 通过调用form表单validate方法来获取验证结果（预验证结果）
      this.$refs.loginFormRef.validate(async validate => {
        if (!validate) return this.$message.warning('用户名或密码错误')
        const { data: res } = await login(this.loginForm)
        if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
        this.$message.success(res.meta.msg)
        this.$store.commit('SAVE_TOKEN', res.data.token)
        this.$router.push('/home')
      })
    }
  }
}
</script>
<style scoped lang="less">
.login {
  width: 100%;
  height: 100%;
  background: #2b4b6b;
  position: relative;
  text-align: center;
  .login-box {
    width: 450px;
    height: 300px;
    background: #fff;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    border-radius: 3px;
    .img_box {
      width: 130px;
      height: 130px;
      padding: 10px;
      border: 1px solid #eee;
      border-radius: 50%;
      box-shadow: 0 0 10px #ddd;
      position: absolute;
      left: 50%;
      transform: translate(-50%,-50%);
      background: #fff;
      img {
        width: 100%;
        height: 100%;
        border-radius: 50%;
        background: #eee;
      };
    };
  }
  .el-form{
    position: absolute;
    bottom: 0;
    padding: 10px;
    box-sizing: border-box;
    width: 100%;
    .form_button{
      display: flex;
      justify-content: flex-end;
    }
  }

}
</style>
